﻿// ***********************************************************************
// Assembly         : NIMServerApi
// Author           : Walter
// Created          : 11-15-2016
//
// Last Modified By : Walter
// Last Modified On : 11-15-2016
// ***********************************************************************
// <copyright file="MsgMethod.cs" company="51iface">
//     Copyright 51iface©  2016
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using NIMServerApi.Commons;
using NIMServerApi.Messages;

namespace NIMServerApi.Methods
{
    /// <summary>
    /// 消息功能类.
    /// </summary>
    /// <seealso cref="NIMServerApi.Methods.BaseMethod" />
    public class MsgMethod:BaseMethod
    {
        public MsgMethod(string appKey, string appSecret):base(appKey,appSecret)
        {

        }

        /// <summary>
        /// 给用户或者群发送普通消息，包括文本，图片，语音，视频和地理位置，具体消息参考下面描述.
        /// </summary>
        /// <param name="from">发送者accid，用户帐号，最大32字符.</param>
        /// <param name="ope">0：点对点个人消息，1：群消息，其他返回414.</param>
        /// <param name="to">To.</param>
        /// <param name="type">The type.</param>
        /// <param name="body">The MSG.</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Paramer 'from' is required.
        /// or
        /// Paramer 'ope' is required.
        /// or
        /// Paramer 'to' is required.
        /// or
        /// Paramer 'body' is required.
        /// </exception>
        public int SendMsg(string from,int ope,string to,MsgType type,string body)
        {
            if (string.IsNullOrEmpty(from))
            {
                throw new ArgumentNullException("Paramer 'from' is required.");
            }
            if (string.IsNullOrEmpty(to))
            {
                throw new ArgumentNullException("Paramer 'to' is required.");
            }
            if (string.IsNullOrEmpty(body))
            {
                throw new ArgumentNullException("Paramer 'body' is required.");
            }
            string postStr = $"from={from}&ope={ope}&to={to}&type={(int)type}&body={body}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/msg/sendMsg.action", postStr, "");
            return base.TryGetJsonValue<int>(json, "code");
        }

        /// <summary>
        /// 批量发送点对点普通消息.
        /// </summary>
        /// <param name="fromAccid">发送者accid，用户帐号，最大32字符.</param>
        /// <param name="type">消息类型.</param>
        /// <param name="body">消息内容.</param>
        /// <param name="toAccids">消息接收人列表.</param>
        /// <returns>SendBatchMsgRetMessage.</returns>
        public SendBatchMsgRetMessage SendBatchMsg(string fromAccid,MsgType type,string body,params string[] toAccids)
        {
            if (string.IsNullOrEmpty(fromAccid))
            {
                throw new ArgumentNullException("Paramer 'fromAccid' is required.");
            }
            if (string.IsNullOrEmpty(body))
            {
                throw new ArgumentNullException("Paramer 'msg' is required.");
            }
            string accids = "";
            accids = string.Join(",",toAccids);
            string postStr = $"fromAccid={fromAccid}&toAccids={accids}&type={(int) type}&body={body}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/msg/sendBatchMsg.action", postStr, "");
            return json.JsonToObject<SendBatchMsgRetMessage>();
        }


    }
}